// 获取抢购数据
export const loadSeckillsInfo = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = [
      {
        intro: '【格调满满日式面包】\n' +
          '口味：黑芝麻麻薯味、蛋黄南瓜味、紫薯栗子味',
        img: 'static/img/index/seckill/inm_food5.jpg',
        price: 7,
        realPrice: 10
      },
      {
        intro: '【青豆米汉堡】甜滋滋的照烧鸡腿肉与圆滚滚的小青豆。',
        img: 'static/img/index/seckill/inm_food2.jpg',
        price: 8,
        realPrice: 11
      },
      {
        intro: '【海苔鸡排饭团】带着一点清新甜味的照烧鸡排，完美的早餐选择。',
        img: 'static/img/index/seckill/inm_food3.jpg',
        price: 7,
        realPrice: 10
      },
      {
        intro: '【芒果味夹心半熟乳酪蛋糕】选用法国芒果果茸，搭配马斯卡邦尼芝士。',
        img: 'static/img/index/seckill/inm_food6.jpg',
        price: 24,
        realPrice: 30
      }

    ];
    const date = new Date();
    const hours = date.getHours();
    const minute = date.getMinutes();
    const seconds = date.getSeconds();
    console.log([hours, minute, seconds]);
    // 距离开始秒杀时间
    const deadline = {
      hours: 1,
      minute: 38,
      seconds: 36
    };
    commit('SET_SECKILLS_INFO', [data, deadline]);
  });
};
// 获取轮播(营销)图片
export const loadServiceItems = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = {
      serviceItems:
        [
          {
            route: '/store',
            img: 'static/img/index/icon/location1.svg',
            title: '附近门店'
          },
          {
            route: '/shoppingCart',
            img: 'static/img/index/icon/cart1.svg',
            title: '购物车'
          },
          {
            route: '/coupon',
            img: 'static/img/index/icon/coupon2.svg',
            title: '优惠券'
          },
          {
            route: '/orderList',
            img: 'static/img/index/icon/order_cute.svg',
            title: '历史订单'
          },
          {
            route: '/pointShop',
            img: 'static/img/index/icon/shop1.svg',
            title: '积分商城'
          },
          {
            route: '/',
            img: 'static/img/index/icon/user3.svg',
            title: '用户中心'
          }
        ],
      userInfo: {
        img: 'static/img/index/icon/user2.svg',
        userName: 'luckku'
      }
    };
    commit('SET_SERVICE_INFO', data);
  });
};
// 获取轮播(营销)图片
export const loadCarouselItems = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = {
      carouselItems: [
        'static/img/nav/1.jpg',
        'static/img/nav/2.jpg',
        'static/img/nav/3.jpg',
        'static/img/nav/4.jpg',
        'static/img/nav/5.jpg'
      ],
      activity: [
        'static/img/nav/nav_showimg1.jpg',
        'static/img/nav/nav_showimg2.jpg'
      ]
    };
    commit('SET_CAROUSELITEMS_INFO', data);
  });
};

// 加载爱吃专栏数据
export const loadEat = ({commit}) => {
  return new Promise((resolve, reject) => {
    const eat = {
      title: '一鸣爆款',
      detail: [
        {
          itemFour: [
            {
              title: '经典羊皮卷',
              intro: '羊皮卷里藏着小秘密：酥香肉松抱着甜腻的奶油在偷笑，等你找到它。',
              img: 'static/img/index/eat/inm_food1.png',
              price: 8
            },
            {
              title: '鸡肉卷',
              intro: '香嫩的蛋皮，裹了番茄酱味儿的鸡柳、鸡蛋，进食原来可以这样生动有趣。',
              img: 'static/img/index/eat/inm_food2.png',
              price: 9
            },
            {
              title: '奶酪小餐包',
              intro: '奶酪馅的小餐包有大能量',
              img: 'static/img/index/eat/inm_food3.png',
              price: 12
            },
            {
              title: '海苔鸡排饭团',
              intro: '带着一点清新甜味的照烧鸡排，与肉松、火腿片来了一次亲密拥抱，让这份米类早餐瞬间丰盛了一整个早晨。',
              img: 'static/img/index/eat/inm_food8.png',
              price: 9
            }
          ]
        },
        {
          itemFour: [
            {
              title: '牛奶手撕面包',
              intro: '浓郁的奶香味、柔软的口感、香甜的风味，是美味晚餐的又一个新的选择。',
              img: 'static/img/index/eat/inm_food5.png',
              price: 10
            },
            {
              title: '乳酪三明治',
              intro: '用满口香滑的乳酪，填补心跟胃吧',
              img: 'static/img/index/eat/inm_food6.png',
              price: 7
            },
            {
              title: '甜甜圈',
              intro: '童年的小心愿，一个甜甜圈足矣',
              img: 'static/img/index/eat/inm_food4.png',
              price: 7
            },
            {
              title: '奥尔良烤鸡大口饭团',
              intro: '沙拉的甜香中和了奥尔良鸡排的饱满肉质口感，生菜、粳米的口感层层递进，每一口都是奇妙体验',
              img: 'static/img/index/eat/inm_food7.png',
              price: 8
            }
          ]
        }
      ]
    };
    commit('SET_EAT_INFO', eat);
  });
};

// 加载积分商城数据
export const loadPointShop = ({commit}) => {
  return new Promise((resolve, reject) => {
    const pointShop = {
      title: '积分商城',
      detail: [
        {
          itemFour: [
            {
              title: '经典羊皮卷',
              intro: '羊皮卷里藏着小秘密：酥香肉松抱着甜腻的奶油在偷笑，等你找到它。',
              img: 'static/img/index/eat/inm_food1.png',
              price: 8
            },
            {
              title: '鸡肉卷',
              intro: '香嫩的蛋皮，裹了番茄酱味儿的鸡柳、鸡蛋，进食原来可以这样生动有趣。',
              img: 'static/img/index/eat/inm_food2.png',
              price: 9
            },
            {
              title: '奶酪小餐包',
              intro: '奶酪馅的小餐包有大能量',
              img: 'static/img/index/eat/inm_food3.png',
              price: 12
            },
            {
              title: '海苔鸡排饭团',
              intro: '带着一点清新甜味的照烧鸡排，与肉松、火腿片来了一次亲密拥抱，让这份米类早餐瞬间丰盛了一整个早晨。',
              img: 'static/img/index/eat/inm_food8.png',
              price: 9
            }
          ]
        },
        {
          itemFour: [
            {
              title: '牛奶手撕面包',
              intro: '浓郁的奶香味、柔软的口感、香甜的风味，是美味晚餐的又一个新的选择。',
              img: 'static/img/index/eat/inm_food5.png',
              price: 10
            },
            {
              title: '乳酪三明治',
              intro: '用满口香滑的乳酪，填补心跟胃吧',
              img: 'static/img/index/eat/inm_food6.png',
              price: 7
            },
            {
              title: '甜甜圈',
              intro: '童年的小心愿，一个甜甜圈足矣',
              img: 'static/img/index/eat/inm_food4.png',
              price: 7
            },
            {
              title: '奥尔良烤鸡大口饭团',
              intro: '沙拉的甜香中和了奥尔良鸡排的饱满肉质口感，生菜、粳米的口感层层递进，每一口都是奇妙体验',
              img: 'static/img/index/eat/inm_food7.png',
              price: 8
            }
          ]
        }
      ]
    };
    commit('SET_POINT_SHOP', pointShop);
  });
};

// 添加购物车
export const addShoppingCart = ({commit}, data) => {
  return new Promise((resolve, reject) => {
    commit('ADD_SHOPPING_CART', data);
  });
};

export const loadAddress = ({commit}) => {
  return new Promise((resolve, reject) => {
    const address = [
      {
        addressId: '123456',
        name: 'Gavin',
        province: '广东省',
        city: '广州市',
        area: '天河区',
        address: '燕岭路633号',
        phone: '152****0609',
        postalcode: '510000'
      },
      {
        addressId: '123458',
        name: 'Kevin',
        province: '上海市',
        city: '上海市',
        area: '浦东新区',
        address: '沙新镇',
        phone: '158****0888',
        postalcode: '200120'
      }
    ];
    commit('SET_USER_ADDRESS', address);
  });
};

// 加载购物车
export const loadShoppingCart = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = {
      totalPrice: 20,
      itemList: [
        {
          goods_id: 1529931938150,
          count: 1,
          img: 'static/img/index/eat/inm_food7.png',
          package: '默认装',
          price: 8,
          title: '奥尔良烤鸡大口饭团',
          description: '沙拉的甜香中和了奥尔良鸡排的饱满肉质口感，生菜、粳米的口感层层递进，每一口都是奇妙体验'
        },
        {
          goods_id: 1529931938150,
          count: 2,
          img: 'static/img/index/eat/inm_food4.png',
          package: '巧克力味',
          price: 7,
          title: '甜甜圈',
          description: '童年的小心愿，一个甜甜圈足矣'
        }
      ]
    };
    commit('SET_SHOPPING_CART', data);
  });
};

// 加载购物车
export const loadOrderList = ({commit}) => {
  return new Promise((resolve, reject) => {
    const orderList = [
      {
        orderId: '1003',
        orderState: '已完成',
        usePoint: false,
        payTime: '2021/12/3 18:00',
        orderCost: '20',
        discount: '2',
        orderItem: [
          {
            goods_id: 1529931938150,
            count: 1,
            img: 'static/img/index/eat/inm_food7.png',
            package: '默认装',
            price: 8,
            title: '奥尔良烤鸡大口饭团',
            description: '沙拉的甜香中和了奥尔良鸡排的饱满肉质口感，生菜、粳米的口感层层递进，每一口都是奇妙体验'
          },
          {
            goods_id: 1529931938150,
            count: 2,
            img: 'static/img/index/eat/inm_food4.png',
            package: '巧克力味',
            price: 7,
            title: '甜甜圈',
            description: '童年的小心愿，一个甜甜圈足矣'
          }]
      },
      {
        orderId: '1002',
        orderState: '已完成',
        usePoint: true,
        payTime: '2021/12/3 17:00',
        pointCost: 8,
        orderCost: null,
        discount: null,
        orderItem: [
          {
            goods_id: 1529931938150,
            count: 1,
            img: 'static/img/index/eat/inm_food7.png',
            package: '默认装',
            price: 8,
            title: '奥尔良烤鸡大口饭团',
            description: '沙拉的甜香中和了奥尔良鸡排的饱满肉质口感，生菜、粳米的口感层层递进，每一口都是奇妙体验'
          }]
      },
      {
        orderId: '1001',
        orderState: '已完成',
        usePoint: false,
        payTime: '2021/12/3 18:00',
        orderCost: '20',
        discount: '2',
        orderItem: [
          {
            goods_id: 1529931938150,
            count: 1,
            img: 'static/img/index/eat/inm_food1.png',
            package: '默认装',
            price: 8,
            title: '羊皮卷',
            description: '羊皮卷里藏着小秘密：酥香肉松抱着甜腻的奶油在偷笑，等你找到它。'
          },
          {
            goods_id: 1529931938150,
            count: 2,
            img: 'static/img/index/eat/inm_food2.png',
            package: '默认装',
            price: 7,
            title: '鸡肉卷',
            description: '香嫩的蛋皮，裹了番茄酱味儿的鸡柳、鸡蛋，进食原来可以这样生动有趣。'
          }]
      }
    ];
    commit('SET_ORDER_LIST', orderList);
  });
};

// 获取优惠券
export const loadCoupon = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = [
      {
        coupon_id: 1529931938150,
        discount: 3,
        condition: 20,
        startTime: '2021-12-15',
        endTime: '2021-12-30',
        title: '满20-3优惠券'
      },
      {
        coupon_id: 1529931938150,
        discount: 2,
        condition: 10,
        startTime: '2021-12-17',
        endTime: '2021-1-1',
        title: '满10-2优惠券'
      }
    ];
    commit('SET_COUPON', data);
  });
};

// 获取优惠券
export const loadStore = ({commit}) => {
  return new Promise((resolve, reject) => {
    const data = [
      {
        img: 'static/img/store/inm_store1.png',
        title: '一鸣真鲜奶吧(雅加弄店)',
        address: '杭州市上城区婺江路近江时代大厦'
      },
      {
        img: 'static/img/store/inm_store2.png',
        title: '一鸣真奶鲜吧(婺江路店)',
        address: '杭州市余杭区70号路鲜果园北200米'
      },
      {
        img: 'static/img/store/inm_store3.png',
        title: '一鸣真鲜奶吧(海创园店)',
        address: '浙江省杭州市余杭区五常街道文一西路998号未来科技城海创园19栋'
      }

    ];
    commit('SET_STORE', data);
  });
};

// 添加注册用户
export const addSignUpUser = ({commit}, data) => {
  return new Promise((resolve, reject) => {
    const userArr = localStorage.getItem('users');
    let users = [];
    if (userArr) {
      users = JSON.parse(userArr);
    }
    users.push(data);
    localStorage.setItem('users', JSON.stringify(users));
  });
};

// 用户登录
export const login = ({commit}, data) => {
  return new Promise((resolve, reject) => {
    if (data.username === 'Gavin' && data.password === '123456') {
      localStorage.setItem('loginInfo', JSON.stringify(data));
      commit('SET_USER_LOGIN_INFO', data);
      resolve(true);
      return true;
    }
    const userArr = localStorage.getItem('users');
    console.log(userArr);
    if (userArr) {
      const users = JSON.parse(userArr);
      for (const item of users) {
        if (item.username === data.username) {
          localStorage.setItem('loginInfo', JSON.stringify(item));
          commit('SET_USER_LOGIN_INFO', item);
          resolve(true);
          break;
        }
      }
    } else {
      resolve(false);
    }
  });
};

// 退出登陆
export const signOut = ({commit}) => {
  localStorage.removeItem('loginInfo');
  commit('SET_USER_LOGIN_INFO', {});
};

// 判断是否登陆
export const isLogin = ({commit}) => {
  const user = localStorage.getItem('loginInfo');
  if (user) {
    commit('SET_USER_LOGIN_INFO', JSON.parse(user));
  }
};
